package com.crayon.utils
{
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.System;
	import flash.utils.ByteArray;
	
	/**
	 * @author 蜡笔
	 */
	public class IPAddress
	{
		
		public function IPAddress()
		{
		
		}
		
		/**
		 * 根据腾讯API查询当前IP地址
		 * @return ["IP","省份","城市"]
		 */
		static public function byTencentAPI():Array
		{
			System.useCodePage = true;
			var urlLoad:URLLoader = new URLLoader();
			var arrIPAddress:Array = [];
			urlLoad.addEventListener(Event.COMPLETE, function():void
				{
					if (urlLoad.data)
					{
						var reg:RegExp = /".*"/ig;
						var sData:String = urlLoad.data;
						arrIPAddress = sData.match(reg);
					}
					urlLoad.removeEventListener(Event.COMPLETE, arguments.callee);
				});
			urlLoad.load(new URLRequest('http://fw.qq.com/ipaddress'));
			return null;
		}
		
		/**
		 * 根据新浪API查询当前IP地址
		 * @return {"country","province","city","desc"}
		 */
		static public function bySinaAPI():Object
		{
			//var remote_ip_info = {"ret":1,"start":"27.154.0.0","end":"27.154.127.255","country":"\u4e2d\u56fd","province":"\u798f\u5efa","city":"\u53a6\u95e8","district":"","isp":"\u7535\u4fe1","type":"","desc":"\u7535\u4fe1\u4e13\u7ebf\u7528\u6237"};
			var urlLoad:URLLoader = new URLLoader();
			urlLoad.addEventListener(Event.COMPLETE, function():void
				{
					if (urlLoad.data)
					{
						var reg:RegExp = /".*"/ig;
						var sData:String = urlLoad.data;
						var str:String = sData.match(reg).join(",");
						var arr:Array = str.split(",");
						var obj:Object = {};
						for each (var item:String in arr)
						{
							obj[item.split(":")[0].replace(/\"/g, "")] = item.split(":")[1].replace(/\"/g, "");
						}
						//return obj;
					}
					urlLoad.removeEventListener(Event.COMPLETE, arguments.callee);
				});
			urlLoad.load(new URLRequest('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js'));
			return null;
		}
	
	}

}